/********************************************************************************************************
 * @file AdvertisingDeviceListAdapter.java
 *
 * @brief for TLSR chips
 *
 * @author telink
 * @date Sep. 30, 2019
 *
 * @par Copyright (c) 2019, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
 *
 *          Licensed under the Apache License, Version 2.0 (the "License");
 *          you may not use this file except in compliance with the License.
 *          You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 *          Unless required by applicable law or agreed to in writing, software
 *          distributed under the License is distributed on an "AS IS" BASIS,
 *          WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *          See the License for the specific language governing permissions and
 *          limitations under the License.
 *******************************************************************************************************/
package com.telink.lt.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.telink.lt.AdvertisingDevice;
import com.telink.lt.R;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class AdvertisingDeviceListAdapter extends BaseRecyclerViewAdapter<AdvertisingDeviceListAdapter.ViewHolder> {
    List<AdvertisingDevice> mDevices;
    Context mContext;

    public AdvertisingDeviceListAdapter(Context context, List<AdvertisingDevice> devices) {
        mContext = context;
        mDevices = devices;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_device_list, parent, false);
        ViewHolder holder = new ViewHolder(itemView);
        holder.tv_name = itemView.findViewById(R.id.tv_name);
        holder.tv_mac = itemView.findViewById(R.id.tv_mac);
        holder.tv_rssi = itemView.findViewById(R.id.tv_rssi);
        return holder;
    }

    @Override
    public int getItemCount() {
        return mDevices == null ? 0 : mDevices.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        AdvertisingDevice device = mDevices.get(position);
        if (TextUtils.isEmpty(device.device.getName())) {
            holder.tv_name.setText("N/A");
        } else {
            holder.tv_name.setText(device.device.getName());
        }
//        holder.tv_name.setText(parseName(mDevices.get(position).scanRecord));
        holder.tv_mac.setText(device.device.getAddress());
        holder.tv_rssi.setText("Rssi: " + mDevices.get(position).rssi + " dBm");
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_name;
        public TextView tv_mac;
        public TextView tv_rssi;

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}
